require(['loading'], function(loading) {
	var params = getUrlParams();
	var pageSize = 20; // 每个页签用的每页显示记录条数大小
	var pageNo = 1; // 每个页签用的页码记录
	var noMore = false; // 没有更多内容啦标志
	var type = ''; // 1/空字符串：全部   2：待付款   3：待收货   4：已完成
	type = params.type?params.type:'';

	//tab切换
	$('.tabs span').on('click', function(){
		var ifActive = $(this).attr('class');
		if(ifActive == 'active'){
			return false;
		} else {
			$(this).addClass('active').siblings().removeClass('active');
			type = $(this).attr("type");
			allOrders.type = type;
			pageNo = 1;
			noMore = false;
			allOrders.moveShowOrders(false);
			if(type == 2){
				if(allOrders.waitPayOrders.length == 0){
					allOrders.noData = true;
				}else{
					allOrders.noData = false;
				}
			}else if(type == 3){
				if(allOrders.waitReceiveOrders.length == 0){
					allOrders.noData = true;
				}else{
					allOrders.noData = false;
				}
			}else if(type == 4){
				if(allOrders.doneOrders.length == 0){
					allOrders.noData = true;
				}else{
					allOrders.noData = false;
				}
			}else{
				if(allOrders.allOrders.length == 0){
					allOrders.noData = true;
				}else{
					allOrders.noData = false;
				}
			}
		}
	});

	var allOrders = new Vue({
		el: '#allOrders',
		data: {
			showOrders:[],
			allOrders:[],
			waitPayOrders:[],
			waitReceiveOrders:[],
			doneOrders:[],
			type: type,
			noData: false,
			showLoading: false,
			item: {
				params:{
					pageSize:200, // 一下子从服务器端加载的页大小
					pageNo:1, // 从服务器端获取订单的页码记录
					token: token,
					userToken: userToken
				}
			},
			orderListUrl: wxUrl + '/v3.2/order/listOrder'
		},
		ready: function() {
			this.getAllOrderList();
			bindScroll();
		},
		methods: {
			getAllOrderList:function(){
				this.$http.get(this.orderListUrl, this.item)
					.then(function(response) {
						var result = JSON.parse(response.body);
						console.log(result)
						var allOrders = result.data.orderList.result;
						if(allOrders.length == 0 && this.item.params.pageNo == 1){
							this.noData = true;
						}
						//this.$set('allOrders', allOrders);
						for (var orderIndex in allOrders) {
							var statuz = allOrders[orderIndex].orderStatus;
							if (statuz == 10){
								allOrders[orderIndex].orderStatusName = '等待卖家确认';
							} else if (statuz == 20 ) { // 待支付
								this.waitPayOrders.push(allOrders[orderIndex]);
								allOrders[orderIndex].orderStatusName = '前往支付';
							} else if (statuz == 30) {
								this.waitReceiveOrders.push(allOrders[orderIndex]);
								allOrders[orderIndex].orderStatusName = '确认打款给卖家';
							} else if (statuz == 40 || statuz == 60 || statuz == 65 || statuz == 70 ) {
								this.doneOrders.push(allOrders[orderIndex]);
							}
							this.allOrders.push(allOrders[orderIndex]);
						}
						this.moveShowOrders(true);
					},function(response) {
						//errorCallback
					});
			},
			moveShowOrders :function(flag){
				var needTotalNum = pageSize * pageNo;
				var oldNeedTotalNum = needTotalNum;
				if (this.type == "2") {
					if (this.waitPayOrders.length < needTotalNum && !flag) {
						this.item.params.pageNo++;
						this.getAllOrderList();
					}
					needTotalNum = this.waitPayOrders.length < needTotalNum?this.waitPayOrders.length:needTotalNum;
					this.$set("showOrders",this.waitPayOrders.slice(0,needTotalNum));
				} else if (this.type == '3') {
					if (this.waitReceiveOrders.length < needTotalNum && !flag) {
						this.item.params.pageNo++;
						this.getAllOrderList();
					}
					needTotalNum = this.waitReceiveOrders.length < needTotalNum?this.waitReceiveOrders.length:needTotalNum;
					this.$set("showOrders",this.waitReceiveOrders.slice(0,needTotalNum));
				} else if (this.type == '4') {
					if (this.doneOrders.length < needTotalNum && !flag) {
						this.item.params.pageNo++;
						this.getAllOrderList();
					}
					needTotalNum = this.doneOrders.length < needTotalNum?this.doneOrders.length:needTotalNum;
					this.$set("showOrders",this.doneOrders.slice(0,needTotalNum));
				} else {
					if (this.allOrders.length < needTotalNum && !flag) {
						this.item.params.pageNo++;
						this.getAllOrderList();
					}
					needTotalNum = this.allOrders.length < needTotalNum?this.allOrders.length:needTotalNum;
					this.$set("showOrders",this.allOrders.slice(0,needTotalNum));
				}
				noMore = oldNeedTotalNum > needTotalNum;
			}
		}
	});

	function bindScroll(){
        window.onscroll = function(){
            if($(window).scrollTop() + $(window).height() >= $(document).height()) {
                if (!noMore) {
					pageNo++;
//					console.info("pageNo:" + pageNo);
					allOrders.moveShowOrders(false);
				} else {
					promptBox("没有更多内容啦！");
				}
            }
        }
    }
		
	// 为可控制返回指定页面所有添加pushState
	history.pushState({}, document.title, null);
	//pushVHistory(self_base_path+"/userCenter/personalData/personal.html");
	pushVHistory();
	window.addEventListener("popstate", function() {
		// 跳转到订单列表页面
		goBackVHistory();
	});
});